package GameObjs;

import loader.GameLoader;
import manager.ElementManager;
import manager.ImageManager;
import manager.LayerManager;
import render.Model;
import render.SimpleModel;

/**
 * 
 * @author YOU��
 *
 */
public class Player extends Actor{

	private Weapon weapon;
	private int direction;
	private boolean left=false; //左
	private boolean right=false;//右
	private boolean jp=false;
	private boolean pkType=false;
	private static long thistime=System.currentTimeMillis();
	private long ty=0;//跳跃时间
	private long tytime=0;//移动时间
	@Override
	public void keypress(int key,boolean bl) { //只有按下或者鬆開才會 调用这个方法
//		System.out.println("测试："+key);
		if(bl) {//按下
			switch(key) {  //怎么优化 大家中午思考;加 监听会持续触发；有没办法触发一次
			case 65: 
				
				this.right=false;this.left=true; break;
			case 68: 
				this.right=true;this.left=false;break;
			case 87:
				this.jp=true;break;//开启跳跃状态
			}
		}else {
			switch(key) {
			case 65: this.left=false;  break;
			
			case 68: this.right=false; break;
			
			}
		//a a
		}	
	}
	@Override
	public void mouse(boolean bl) {
		if(bl==true)
		this.pkType=true;
		else {
			this.pkType=false;
		}
	}
	
	@Override
	void attack() {
		if(!this.pkType) {//如果是不发射状态 就直接return
			return;
		}
		this.pkType=false;
		System.out.println("发射");
		// TODO Auto-generated method stub
		ElementManager em = ElementManager.get();
		LayerManager lManager = LayerManager.get();	
		ImageManager im = ImageManager.get();
		im.addImage("images/Enemy/20070130041731OFQDOJuj-104.png");
		Model model = new SimpleModel("20070130041731OFQDOJuj-104.png-Enemy");
		
		Bullet zd=new Bullet(getX(),getY());
		zd.setModel(model);
		em.add(ElementType.ITEM, zd);
		lManager.setLayer(zd, 0, zd.getZ());
		System.out.println("成功");
		
	}
	@Override
	protected void move() {
		// TODO 自动生成的方法存根
		if (this.left ) {
//			zb++;
//			if(zb==5000) {
//				this.setX(getX()-1);
//				zb=0;
//			}
			if(System.currentTimeMillis()%10==0) {
				if(thistime!=System.currentTimeMillis()) {
					this.setX(getX()-1);
					thistime=System.currentTimeMillis();
				}
				
			}
		}
		if (this.right) {  //坐标的跳转由大家来完成
//			zb++;
//			if(zb==5000) {
//				this.setX(getX()+1);
//				zb=0;
//			}
			if(System.currentTimeMillis()%10==0) {
				if(thistime!=System.currentTimeMillis()) {
					this.setX(getX()+1);
					thistime=System.currentTimeMillis();
				}
			}
		}
	}
	
	@Override
	final public void ObjRun ()
	{
		
		move();
		jump();
		attack();
		//System.out.println("位置"+this.getX());
	}
	
	public void closeAttack()
	{
		
	}
	
	public void jump ()
	{
		if(jp==false)return;
		else
		 {
			if(ty<System.currentTimeMillis()-1050)
			ty=System.currentTimeMillis();
			if(System.currentTimeMillis()-ty>1000) {
				jp=false;
			}
			else {
				if(System.currentTimeMillis()-ty<=400){
					//if(ty%2000==0)
					if(System.currentTimeMillis()%10==0) {
						if(tytime!=System.currentTimeMillis()) {
							this.setY(getY()-2);
							tytime=System.currentTimeMillis();
						}
						
					}
				}
				else if(System.currentTimeMillis()-ty>600) {
					//if(ty%2000==0)
					if(System.currentTimeMillis()%10==0) {
						if(tytime!=System.currentTimeMillis()) {
							this.setY(getY()+2);
							tytime=System.currentTimeMillis();
						}
						
					}
				}
			}
		}
	}
	
	@Override
	public void death()
	{
		
	}

	public Weapon getWeapon() {
		return weapon;
	}

	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	
	
}
